<?php

require_once('include/include_base.php');

require_once('include/precondition_no_loggedin.php');


// check for a submit form
if ( (isset($_POST['action']) && $_POST['action'] == "login") ) {

	$user = new User(new UserVO($_POST));
	
	// validate input data (username and password)
	if ($user->validateLogin()) {
	
		// get User by username+password 
		if ($user->login()) {

			// check if it exists a URL to redirect
			if (!Validation::isEmptyString($_POST['redirect']))
				header('Location: ' . Utilities::deEncodeURL($_POST['redirect']) );
			else 
				header('Location: '.BASE_URL .'/home');
			exit();
			
		} else {
			$smarty->assign("userVO", $user->userVO);
			$smarty->assign("messages", Utilities::i18nMessages($user->messages, $smarty));
		}
	}
	else {
		
		$smarty->assign("userVO", $user->userVO);
		//$smarty->assign("messages", Utilities::i18nMessages($user->messages, $smarty));
		$smarty->assign("messages", $user->messages);
	}
	
}



// ----------------


$smarty->assign("title", "Sign in to");
$smarty->assign("page", "login");
$smarty->display('master.tpl.html');